//
//  DetailViewController.m
//  EverydayDemo
//
//  Created by qianfeng01 on 15-7-19.
//  Copyright (c) 2015年 qianfeng01. All rights reserved.
//

#import "DetailViewController.h"
#import <WebKit/WebKit.h>
#import "DBManager.h"
#import "HomePageModel.h"
@interface DetailViewController ()
{
    UIWebView *_webView;
}
@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
    button.frame=CGRectMake(0, 0, 60, 30);
    [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
 
    BOOL isExist=[[DBManager sharedManager]isExistAppForAppId:self.articleId];
    if (isExist) {
        button.enabled=NO;
        
    }else{
        button.enabled=YES;
    }
    [button setTitle:@"已收藏" forState:UIControlStateDisabled];
    [button setTitle:@"收藏" forState:UIControlStateNormal];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = item;
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, -44, kScreenSize.width,kScreenSize.height+44)];
    //处理UI显示的代理
    //_webView.navigationDelegate = self;
    //加载网页
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.url]];
    //加载请求
    [_webView loadRequest:request];
    [self.view addSubview:_webView];
   
}
-(void)btnClick:(UIButton *)button
{
    button.enabled = NO;
    BOOL isExist=[[DBManager sharedManager]isExistAppForAppId:self.articleId];
    if (isExist) {
        [self showActionHSeet];
        return;
    }
    //收藏 记录到本地数据库
    HomePageModel *model=[[HomePageModel alloc]init];
    model.imageUrl=self.imageUrl;
    model.title=self.Artitle;
    model.categryName=self.categryName;
    model.authorName=self.authorName;
    model.readCount=self.readCount;
    model.articleId=self.articleId;
    model.url=self.url;
    [[DBManager sharedManager] insertModel:model];
}
-(void)showActionHSeet{
    UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:nil message:@"您已经收藏过啦" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    alertView.alertViewStyle=UIAlertControllerStyleActionSheet;
    [alertView show];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
